//
//  ShouCangViewController.m
//  Delicious2
//
//  Created by dllo on 16/1/11.
//  Copyright © 2016年 apple. All rights reserved.
//

#import "ShouCangViewController.h"
#import "ShouCangTableViewCell.h"
#import "EatShowViewController.h"
#import "FSDCollectionTableViewCell.h"

@interface ShouCangViewController ()<UITableViewDataSource, UITableViewDelegate>

@property (nonatomic, strong) UITableView *tableView;
@property (nonatomic, strong) NSMutableArray *array;
@property (nonatomic, strong) DataBaseHandle *dataBaseHandle;
@property (nonatomic, strong) NSMutableArray *dataSource;

@property (nonatomic, strong) NSMutableArray *cookBookArray;
@property (nonatomic, strong) NSMutableArray *eatArray;
@property (nonatomic, strong) NSMutableArray *gourmetArray;
@property (nonatomic, strong) NSMutableArray *meishiArray;

@end

@implementation ShouCangViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    
    self.navigationItem.title = @"我的收藏";
//    self.navigationItem.rightBarButtonItem = self.editButtonItem;
    self.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc] initWithImage:[[UIImage imageNamed:@"SearchImage"] imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal]style:UIBarButtonItemStyleDone target:self action:@selector(rightButton:)];
    
    [self creatTableView];
    [self getDataSource];
}

-(void)getDataSource {
    self.array = [NSMutableArray arrayWithCapacity:0];
    self.dataSource = [NSMutableArray arrayWithCapacity:0];
    self.cookBookArray = [NSMutableArray arrayWithCapacity:0];
    self.eatArray = [NSMutableArray arrayWithCapacity:0];
    self.gourmetArray = [NSMutableArray arrayWithCapacity:0];
    self.meishiArray = [NSMutableArray arrayWithCapacity:0];
    
    self.dataBaseHandle = [DataBaseHandle shareDataBase];
    [self.dataBaseHandle openDataBase];
    self.array = [self.dataBaseHandle selectDataBase:nil];
    [self.dataBaseHandle closeDataBase];
    if (self.array.count <= 0) {
        MBProgressHUD *HUD = [MBProgressHUD showHUDAddedTo:self.view animated:YES];
        HUD.mode = MBProgressHUDModeText;
        HUD.margin = 10;
        HUD.cornerRadius = 3;
        HUD.labelText = @"您还没有收藏";
        [HUD hide:YES afterDelay:1];
    }
    
    for (NSDictionary *dic in self.array) {
        MineModel *mineModel = [[MineModel alloc] initWithDictionary:dic];
        if ([mineModel.kind isEqualToString:@"食谱"]) {
            [self.cookBookArray addObject:mineModel];
            
        } else if ([mineModel.kind isEqualToString:@"吃"]) {
            [self.eatArray addObject:mineModel];
            
        } else if ([mineModel.kind isEqualToString:@"食课"]) {
            [self.gourmetArray addObject:mineModel];
        } else {
            [self.meishiArray addObject:mineModel];
        }
    }
    [self.tableView reloadData];
}

//creat TableView
-(void)creatTableView {
    self.tableView = [[UITableView alloc] initWithFrame:self.view.bounds style:UITableViewStylePlain];
    [self.tableView registerClass:[FSDCollectionTableViewCell class] forCellReuseIdentifier:@"shouCangTableViewCellIdentifer"];
    self.tableView.delegate = self;
    self.tableView.dataSource = self;
    self.tableView.tag = 1000;
//    self.tableView.bounces = NO;
    
    [self.view addSubview:self.tableView];
}

-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    
    FSDCollectionTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"shouCangTableViewCellIdentifer"];
    cell.selectionStyle = UITableViewCellSelectionStyleNone;
    
    [cell nightMode];
    
    if (indexPath.section == 0) {
        cell.mineModel = self.cookBookArray[indexPath.row];

    } else if (indexPath.section == 1) {
        cell.mineModel = self.eatArray[indexPath.row];
  
    } else if (indexPath.section == 2) {
        cell.mineModel = self.gourmetArray[indexPath.row];

    } else {
        cell.mineModel = self.meishiArray[indexPath.row];
    }
    return cell;
}


-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    if (section == 0) {
        return self.cookBookArray.count;
    } else if (section == 1) {
        return self.eatArray.count;
    } else if (section == 2) {
        return self.gourmetArray.count;
    } else {
        return self.meishiArray.count;
    }
}

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    return 4;
}

- (NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section {
    if (section == 0) {
        return @"食谱";
    } else if (section == 1) {
        return @"吃货";
    } else if(section == 2){
        return @"食课";
    } else {
        return @"附近美食";
    }
}

- (CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section {
    return CGFLOAT_MIN;
}

-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
    return 100;
}

-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    
    EatShowViewController *eatShow = [[EatShowViewController alloc] init];
    if (indexPath.section == 0) {
        MineModel *mineModel = self.cookBookArray[indexPath.row];
        eatShow.urlString = mineModel.url;
        eatShow.urlTitle = mineModel.title;
        eatShow.urlImage = mineModel.image;
        eatShow.urlKind = mineModel.kind;
    }
    if (indexPath.section == 1) {
        MineModel *mineModel = self.eatArray[indexPath.row];
        eatShow.urlString = mineModel.url;
        eatShow.urlTitle = mineModel.title;
        eatShow.urlImage = mineModel.image;
        eatShow.urlKind = mineModel.kind;
    }
    if (indexPath.section == 2) {
        MineModel * mineModel = self.gourmetArray[indexPath.row];
        eatShow.urlString = mineModel.url;
        eatShow.urlTitle = mineModel.title;
        eatShow.urlImage = mineModel.image;
        eatShow.urlKind = mineModel.kind;
    }
    if (indexPath.section == 3) {
        MineModel * mineModel = self.meishiArray[indexPath.row];
        eatShow.urlString = mineModel.url;
        eatShow.urlTitle = mineModel.title;
        eatShow.urlImage = mineModel.image;
        eatShow.urlKind = mineModel.kind;
    }
    [self.navigationController pushViewController:eatShow animated:YES];
}

- (void)rightButton:(UIBarButtonItem *)sender {
    CollectionSearchViewController *search = [[CollectionSearchViewController alloc] init];
    [self.navigationController pushViewController:search animated:YES];
}

#pragma mark ----- tableView编辑
-(void)setEditing:(BOOL)editing animated:(BOOL)animated {
    [super setEditing:editing animated:animated];
    //    通过tag获取tableView
    UITableView *tableView = (UITableView *)[self.view viewWithTag:1000];
    //    让tableView处于可编辑状态
    [tableView setEditing:editing animated:animated];
}

//设置tableView的编辑样式
-(UITableViewCellEditingStyle)tableView:(UITableView *)tableView editingStyleForRowAtIndexPath:(NSIndexPath *)indexPath {
    return UITableViewCellEditingStyleDelete;
}
//控制是否允许编辑的协议方法
-(BOOL)tableView:(UITableView *)tableView canEditRowAtIndexPath:(NSIndexPath *)indexPath {
    return YES;
}

-(void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath {
    
    if (editingStyle == UITableViewCellEditingStyleDelete) {
        // 先处理数据
        
        [self.dataBaseHandle openDataBase];
        [self.dataBaseHandle createTable];

        if (indexPath.section == 0) {
            [self.dataBaseHandle deleteDataBase:[self.cookBookArray[indexPath.row] title]];
            [self.cookBookArray removeObjectAtIndex:indexPath.row];
            
        } else if (indexPath.section == 1) {
            [self.dataBaseHandle deleteDataBase:[self.eatArray[indexPath.row] title]];
            [self.eatArray removeObjectAtIndex:indexPath.row];
            
        } else if (indexPath.section == 2) {
            [self.dataBaseHandle deleteDataBase:[self.gourmetArray[indexPath.row] title]];
            [self.gourmetArray removeObjectAtIndex:indexPath.row];
            
        } else if (indexPath.section == 3) {
            [self.dataBaseHandle deleteDataBase:[self.meishiArray[indexPath.row] title]];
            [self.meishiArray removeObjectAtIndex:indexPath.row];
            
        }
        [self.dataBaseHandle closeDataBase];
        [tableView deleteRowsAtIndexPaths:@[indexPath] withRowAnimation:UITableViewRowAnimationRight];
        
        if (self.array.count <= 0) {
            MBProgressHUD *HUD = [MBProgressHUD showHUDAddedTo:self.view animated:YES];
            HUD.mode = MBProgressHUDModeText;
            HUD.margin = 10;
            HUD.cornerRadius = 3;
            HUD.labelText = @"您还没有收藏";
            [HUD hide:YES afterDelay:1];
        }
    }
}

- (void)viewWillAppear:(BOOL)animated {
    [super viewWillAppear:YES];
   
    [self getDataSource];

    [self.view nightMode];
    [self.tableView nightMode];
    
    
    //取消抽屉滑动
    AppDelegate *delegate = (AppDelegate *)[[UIApplication sharedApplication] delegate];
    DrawerViewController *drawerVC = delegate.drawerVC;
    drawerVC.needSwipeShowMenu = NO;
}



//开启抽屉滑动
- (void)viewWillDisappear:(BOOL)animated {
    AppDelegate *delegate = (AppDelegate *)[[UIApplication sharedApplication] delegate];
    DrawerViewController *drawerVC = delegate.drawerVC;
    drawerVC.needSwipeShowMenu = YES;
    
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
